package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYapplyJoinUnionReq;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.message.proto.out.XYapplyJoinUnionRsp;
import com.game.server.message.proto.out.XYunionSerchRsp;
import com.game.server.util.NoticeUtil;
import net.sf.json.JSONObject;

public class XYapplyJoinUnionReq extends AbstractXYapplyJoinUnionReq {


    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYapplyJoinUnionRsp rsp = new XYapplyJoinUnionRsp();
            rsp.setIsok(1);
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
            if (clubInfo!=null && clubInfo.getCreatorid()==client.getUserId()) {
                if (clubInfo.getUnionid() == 0) {
                    XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(this.unionid);
                    if (unionInfo != null) {
                        rsp.setIsok(2);
                        int applyCount = XyNotificationLogDao.findByUidApplyCount(clubInfo.getId(), unionInfo.getId(),2,3);
                        if (applyCount==0){
                            rsp.setIsok(0);
                            XyNotificationLog entity = new XyNotificationLog();
                            entity.setCid(unionInfo.getId());
                            entity.setUid(clubInfo.getId());
                            entity.setNotification_type(2);
                            entity.setMsg_type(3);
                            entity.setState(0);
                            entity.setAdditional_data("");
                            XyNotificationLogDao.insertInto(entity);

                            NoticeUtil.ClubNoticeNotice(client, clubInfo);
                        }
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
